package com.itheima.test2;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    public static void main(String[] args) throws IOException {
        //创建ServerSocket对象并指定端口号
        ServerSocket ss = new ServerSocket(8888);
        //监听客户端的连接请求
        Socket socket = ss.accept();
        //获取输入流
        InputStreamReader isr = new InputStreamReader(socket.getInputStream());
        int i;
        //read没有读到结尾标记会一直在这边等待读取数据，
        //所以要在客户端关闭输出流后才会停止读取数据
        while ((i = isr.read()) != -1) {
            System.out.print((char) i);
        }
        //给客户端反馈消息
        OutputStream os = socket.getOutputStream();
        os.write("收到".getBytes());
//        socket.shutdownOutput();

        //关闭socket和服务器
        socket.close();
        ss.close();

    }
}
